package cn.com.dcsgo.settlement;

import cn.com.dcsgo.settlement.calculator.*;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 订单金额计算器工厂
 *
 * @author Dcsgo
 * @since 2025/11/17 18:53
 */
@Component
public class OrderAmountCalculatorFactory implements ApplicationContextAware {
    /**
     * 计算订单结算金额责任链中的第一个计算器
     */
    private AbstractOrderAmountCalculator firstCalculator;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CommoditiesAmountCalculator commoditiesAmountCalculator = applicationContext.getBean(CommoditiesAmountCalculator.class);
        DiscountMoneyCalculator discountMoneyCalculator = applicationContext.getBean(DiscountMoneyCalculator.class);
        FreightCalculator freightCalculator = applicationContext.getBean(FreightCalculator.class);
        ActualPaymentMoneyCalculator actualPaymentMoneyCalculator = applicationContext.getBean(ActualPaymentMoneyCalculator.class);
        // 设置责任链顺序
        commoditiesAmountCalculator.setNextCalculator(discountMoneyCalculator);
        discountMoneyCalculator.setNextCalculator(freightCalculator);
        freightCalculator.setNextCalculator(actualPaymentMoneyCalculator);

        firstCalculator = commoditiesAmountCalculator;
    }

    public AbstractOrderAmountCalculator getOrderAmountCalculator() {
        return firstCalculator;
    }
}
